import 'package:flutter/widgets.dart';
import 'package:library_base/entities/deliver_goods_entity.dart';
import 'package:library_base/entities/distribution_child_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:get/get.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/channel.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class DeliverGoodsController
    extends BaseListController<DeliverGoodsEntity, DeliverGoodsEntity> {
  String stationTimeBegin = '';
  
  Rx<String> keyWord = ''.obs;

  Rx<bool> isConnected = false.obs;

  double finishedCount = 0.0;

  double totalCount = 0.0;

  bool enable = false;

  RxList<String> stockOutIds = <String>[].obs;

  ///是否全选
  bool isSelectedAll = false;

  TextEditingController keyWorkSearchController = TextEditingController();

  @override
  void onCreate() {
    enableLoadMore = true;
    isShowNoMoreFooter = true;
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": page,
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
    return super.requestData();
  }

  @override
  List<DeliverGoodsEntity> transformObject(
      DeliverGoodsEntity data, bool isRefresh) {
    if (isRefresh) {
      stockOutIds.clear();
    }
    return [DeliverGoodsEntity()];
  }

  void filter(String time) {
    stationTimeBegin = time;
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": getPage(false),
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": getPage(true),
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": getPage(false),
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
  }

  void onSearch(String value) {
    tryReLoad();
  }

  void onChanged(String value) {
    if (keyWord.value != value) {
      keyWord.value = value;
      update();
    }
  }

  void backToList() {
    keyWord.value = '';
    keyWorkSearchController.text = '';
    stationTimeBegin = '';
    tryReLoad();
  }

  void reset() {
    stationTimeBegin = '';
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": getPage(false),
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": getPage(false),
          "pageSize": limit,
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "orderNumber": keyWord.value,
        });
  }

  void onItemClick(DeliverGoodsEntity itemData) {}

  void onExpand(DeliverGoodsEntity itemData) {}

  void onTapChild(DistributionChildEntity itemData) {}

  void toOrderDetail(DeliverGoodsEntity itemData, int position) {
    // Get.toNamed(Routers.ORDER_DETAIL_PAGE,
    //     arguments: {'id': itemData.id, 'callback': tryReLoad});
  }

  ///发货
  void deliverGoods() {
    requestUniversal<bool>(Apis.FINISH_RECORDS,
        tipSuccess: MultiLanguage.successfulDelivery.tr,
        isShowToastTip: true,
        params: {"stockOutIds":stockOutIds}, resultSuccessCall: (result) async {
          tryReLoad();
        });
  }

  ///打印
  void print() {
    if(GetPlatform.isAndroid){
      sendAndroidChannel.invokeMethod(CHECK_BLUETOOTH_IS_OPEN_4);
    }else{
      Get.toNamed(Routers.PRINT_DETAIL_PAGE,arguments: {"ids":stockOutIds});
    }
  }
}

